<template>
  <div class="mapContent">
    <!-- <span @click="addPoint">添加</span> -->
    <div id="container"></div>
  </div>
</template>
<script>
import MapLoader from "@/business/api/map.js";
export default {
  name: "mapComponent",
  data() {
    return {
      map: null,
      marker: null,
      infoWindow: null,
    };
  },
  watch: {
    $route(to, from) {
      console.log(to);
      this.getMap();
    },
  },
  created() {
    this.getMap();
  },
  mounted() {
    this.getMap();
  },
  methods: {
    // getMap() {
    //   let that = this;
    //   MapLoader().then(
    //     (AMap) => {
    //       console.log("地图加载成功");
    //       that.map = new AMap.Map("container", {
    //         center: [117.000923, 36.675807],
    //         zoom: 12,
    //       });
    //       this.instanceInfoForm();
    //       that.addPoint();
    //     },
    //     (e) => {
    //       console.log("地图加载失败", e);
    //     }
    //   );
    // },
    // addPoint() {
    //   let self = this;
    //   //清空地图上的店
    //   self.map.clearMap();
    //   let marker = new AMap.Marker({
    //     // map: this.map,
    //     position: new AMap.LngLat(117.000923, 36.675807),
    //     icon: "http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
    //     offset: new AMap.Pixel(-13, -30),
    //   });
    //   self.map.add(marker);
    //   //鼠标点击marker弹出自定义的信息窗体
    //   AMap.event.addListener(marker, "click", function () {
    //     self.infoWindow.open(self.map, marker.getPosition());
    //   });
    // },
    instanceInfoForm() {
      //实例化信息窗体
      let title = "",
        content = [];
      content.push(
        `
            <div class="headerDiv">
                <div>
                   <span>
                        <label>作业计划名称：</label>
                        <label>11</label> 
                    </span>
                    <span>
                        <label>作业风险等级：</label>
                        <label>22</label>
                    </span>
                    <span>
                        <label>专业类型:</label>
                        <label>33</label>
                    </span>
                </div>
                <div>
                    <span>
                        <label>作业计划名称：</label>
                        <label>11</label> 
                    </span>
                    <span>
                        <label>作业风险等级：</label>
                        <label>22</label>
                    </span>
                    <span>
                        <label>专业类型:</label>
                        <label>33</label>
                    </span>
                </div>
                <div>
                   <span>
                        <label>作业计划名称：</label>
                        <label>11</label> 
                    </span>
                    <span>
                        <label>作业风险等级：</label>
                        <label>22</label>
                    </span>
                    <span>
                        <label>专业类型:</label>
                        <label>33</label>
                    </span>
                </div>
            </div>
            <div class="headerDiv">
                <div>
                    <span>
                        重点关注单位
                    </span>
                    <span>
                        XX单位本年度安全积分剩余3分
                    </span>
                </div>
                <div>
                    <span>
                        重点关注人员
                    </span>
                    <span>
                        XX单位本年度安全积分剩余2分 
                    </span>
                </div>
            </div>
            <div class="footDiv">
                <span>
                    111
                </span>
                <span>
                    111
                </span>
                <span>
                    111
                </span>
            </div>
          `
      );
      this.infoWindow = new AMap.InfoWindow({
        isCustom: true, //使用自定义窗体
        content: this.createInfoWindow(title, content.join("<br/>")),
        offset: new AMap.Pixel(16, -45),
      });
    },
    //构建自定义信息窗体
    createInfoWindow(title, content) {
      let info = document.createElement("div");
      info.className = "custom-info input-card content-window-card";
      //可以通过下面的方式修改自定义窗体的宽高
      info.style.width = "400px";
      //定义顶部标题
      let top = document.createElement("div");
      let titleD = document.createElement("div");
      let closeX = document.createElement("img");
      top.className = "info-top";
      titleD.innerHTML = title;
      closeX.src = "https://webapi.amap.com/images/close2.gif";
      closeX.onclick = this.closeInfoWindow;

      top.appendChild(titleD);
      top.appendChild(closeX);
      info.appendChild(top);

      // 定义中部内容
      let middle = document.createElement("div");
      middle.className = "info-middle";
      middle.style.backgroundColor = "white";
      middle.innerHTML = content;
      info.appendChild(middle);

      // 定义底部内容
      let bottom = document.createElement("div");
      bottom.className = "info-bottom";
      bottom.style.position = "relative";
      bottom.style.top = "0px";
      bottom.style.margin = "0 auto";
      let sharp = document.createElement("img");
      sharp.src = "https://webapi.amap.com/images/sharp.png";
      bottom.appendChild(sharp);
      info.appendChild(bottom);
      return info;
    },
    closeInfoWindow() {
      this.map.clearInfoWindow();
    },
  },
};
</script>
<style scoped>
.mapContent {
  width: 100%;
  height: 100%;
}
#container {
  width: 100%;
  height: 100%;
}

>>> .amap-icon img {
  width: 25px;
  height: 34px;
}
>>> .content-window-card {
  position: relative;
  box-shadow: none;
  bottom: -25px;
  left: 0;
  width: auto;
  padding: 0;
}

>>> .content-window-card p {
  height: 2rem;
}

>>> .custom-info {
  border: solid 1px silver;
}
>>> .headerDiv {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
}
>>> .headerDiv:first-child {
  background-color: darkgrey;
}
>>> .headerDiv div {
  flex: 1;
  margin: 0 5px;
  text-align: right;
}
>>> .headerDiv div span {
  display: block;
}
>>> .footDiv {
  display: flex;
  text-align: center;
  justify-content: space-between;
}
>>> .footDiv span {
  border: 1px solid gray;
  width: 100%;
}
</style>
